package com.epalmpay.controller.spot;

import com.epalmpay.commom.BaseController;
import com.epalmpay.dto.order.OrderInDTO;
import com.epalmpay.dto.system.BaseOutDTO;
import com.epalmpay.entity.Ticket;
import com.epalmpay.exception.ApplicationException;
import com.epalmpay.service.spot.ISpotService;
import com.epalmpay.status.Rescode;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by Administrator on 2018/1/24.
 */

@Controller
@RequestMapping(value="/merchant/spotmanage")
public class SpotManageController extends BaseController {

    @Resource
    private ISpotService spotService;

    /**
     * 根据groupid获取门票类型列表
     *
     */
    @RequestMapping(value="/getTicketsByGroupId")
    @ResponseBody
    public List getTicketsByGroupId(){
        Ticket ticket = new Ticket();
        ticket.setGroupId(Long.parseLong(getRequest().getParameter("groupid")));
        try {
            return spotService.selectTicketsByGroupId(ticket);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 更新门票类型
     *
     */
    @RequestMapping(value="/updateTicket")
    @ResponseBody
    public int updateTicket(){
        Ticket ticket = new Ticket();
        ticket.setId(Long.parseLong(getRequest().getParameter("id")));
        ticket.setTicketname(getRequest().getParameter("ticketname"));
        ticket.setTicketprice(Long.parseLong(getRequest().getParameter("ticketprice")));
        ticket.setShortcutkey(getRequest().getParameter("shortcutkey"));
        try {
            return spotService.updateTicket(ticket);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 添加门票类型
     *
     */
    @RequestMapping(value="/insertTicket")
    @ResponseBody
    public int insertTicket(){
        Ticket ticket = new Ticket();
        ticket.setGroupId(Long.parseLong(getRequest().getParameter("groupId")));
        ticket.setTicketname(getRequest().getParameter("ticketname"));
        ticket.setTicketprice(Long.parseLong(getRequest().getParameter("ticketprice")));
        ticket.setShortcutkey(getRequest().getParameter("shortcutkey"));
        try {
            return spotService.insertTicket(ticket);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 删除门票类型
     *
     */
    @RequestMapping(value="/deleteTicket")
    @ResponseBody
    public int deleteTicket(){
        Ticket ticket = new Ticket();
        ticket.setId(Long.parseLong(getRequest().getParameter("id")));
        try {
            return spotService.deleteTicket(ticket);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 获取门票详情
     *
     */
    @RequestMapping(value="/ticketdetail")
    @ResponseBody
    public Ticket ticketdetail(){
        Ticket ticket = new Ticket();
        ticket.setId(Long.parseLong(getRequest().getParameter("id")));
        try {
            return spotService.selectTicketById(ticket);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 根据门票名称获取门票详情
     *
     */
    @RequestMapping(value="/checkticket")
    @ResponseBody
    public Ticket checkticket(){
        Ticket ticket = new Ticket();
        ticket.setTicketname(getRequest().getParameter("ticketname"));
        ticket.setGroupId(Long.parseLong(getRequest().getParameter("groupId")));
        try {
            return spotService.selectTicketByName(ticket);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    /**
     *
     */
    @RequestMapping(value="/saveSpotData")
    @ResponseBody
    public BaseOutDTO saveSpotData(OrderInDTO orderInDTO){
        BaseOutDTO baseOutDTO = new BaseOutDTO();
        try {
            //保存数据
        	Boolean flag=spotService.saveSpotData(orderInDTO);
        	if(flag){
        		baseOutDTO.setErrorCode(Rescode.SUCCESS, "处理成功");
                return baseOutDTO;
        	}else{
        		baseOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
                return baseOutDTO;
        	}
        }catch (ApplicationException ae) {
            baseOutDTO.setErrorCode(ae.errCode, ae.errMsg);
            ae.printStackTrace();
        }
        catch (Exception e) {
            baseOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
            e.printStackTrace();
        }
        return baseOutDTO;
    }


}
